<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col :span="3" class="left">
        <img src="//s0.meituan.net/bs/fe-web-meituan/fa5f0f0/img/logo.png" alt="美团" />
      </el-col>
      <el-col :span="15" class="center">
        <div class="wrapper">
          <el-input
            v-model="searchWord"
            placeholder="请输入内容"
            @focus="focus"
            @blur="blur"
            @input="input"
          ></el-input>
          <el-button type="primary" icon="el-icon-search"></el-button>
          <dl class="hotPlace" v-if="isHotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item,index) in hotPlaceList" :key="index">
              <router-link :to="{name: 'goods', params:{name: item}}">{{ item }}</router-link>
            </dd>
          </dl>
          <dl class="searchList" v-if="isSearchList">
            <dd v-for="(item,index) in searchList" :key="index">
              <router-link :to="{name: 'goods', params:{name: item}}">{{ item }}</router-link>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <a href="#" v-for="(item,index) in suggestList" :key="index">{{ item }}</a>
        </p>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import api from "@/api/index.js";

export default {
  data() {
    return {
      searchWord: "",
      isFoucs: false,
      hotPlaceList: [
        "京东第一温泉酒店",
        "99旅馆连锁",
        "尚客优快捷酒店",
        "7天连锁酒店"
      ],
      searchList: ["火锅", "火锅自助餐", "火锅外卖"],
      suggestList: [
        "京东第一温泉酒店",
        "99旅馆连锁",
        "尚客优快捷酒店",
        "7天连锁酒店"
      ]
    };
  },
  computed: {
    isHotPlace: function() {
      return this.isFoucs && !this.searchWord;
    },
    isSearchList: function() {
      return this.isFoucs && this.searchWord;
    }
  },
  methods: {
    focus() {
      this.isFoucs = true;
    },
    blur() {
      let self = this;
      setTimeout(() => {
        self.isFoucs = false;
      }, 200);
    },
    input() {
      let val = this.searchWord;
        api.getSearchList().then(res => {
          this.searchList = res.data.data.list.filter((item, index) => {
              return item.indexOf(val) > -1;
          });
        });
    }
  },
  created() {
    api.getSearchHotWord().then(res => {
      this.hotPlaceList = res.data.data;
      this.suggestList = res.data.data;
    });
  },
  mounted() {}
};
</script>

<style lang="scss">
@import "@/assets/css/public/header/search.scss";
@import "@/assets/css/public/header/index.scss";
</style>